import Vue from "vue";
import Vuex from "vuex";
import axios from 'axios'
Vue.use(Vuex);
let userInfo=localStorage.getItem("userInfo");
userInfo=JSON.parse(userInfo)
export default new Vuex.Store({
  state: {

    userInfo:userInfo,
    
    headNav1:'head-nav1',
    head: {
			toggle: false,
			title: '',
      style: {'background': 'rgba(43,162,251,0)'},
      style2:{'color':'#fff'}
    },
    listInfo: {
			songList: [],
			songIndex: 0
    },
   
    audio: {
			songUrl: '',
			// imgUrl: './assets/images/b.png',
      // imgUrl: 'http://m.kugou.com/v3/static/images/index/logo_kugou.png',
			title: '',
			singer: '',
			currentLength: 0,
			songLength: 0,
      currentFlag: false,
      song_name:'',
      lrc:'',
    },
    showPlayer: false,
    listenCount: 0,
    isPlay: true,
    detailPlayerFlag: false,
<<<<<<< HEAD
    isTitle:false,
    bottomBar:false
=======
    menu:false
>>>>>>> 7dd98ad4f10b0d0fc1a7994a24c127972341b2d9
  },
  mutations: {
    setUserInfo(state,payload){
      state.userInfo=payload
      localStorage.setItem("userInfo",JSON.stringify(payload))
    },
    setHeadNav:(state,nav)=>{
      state.headNav1=nav
    },
    setHeadTitle(state, title){
			state.head.title = title
    },
    showHead(state, flag){
			state.head.toggle = flag
    },
    setHeadStyle(state, style){
			state.head.style = style
    },
    resetHeadStyle: state => {
			state.head.style = {'background': 'rgba(0,0,0,0)'}
		},
    setListInfo: (state, {songList, index}) => {
			state.listInfo.songList = songList
			state.listInfo.songIndex = index
    },
    setLrc: (state, lrc) => {
			state.audio = {...(state.audio), lrc}
    },
    setAudio(state, audio){
			if (!state.listenCount) {
				state.showPlayer = true  //首次进入应用时不可打开播放详情
			}
			state.listenCount++
			state.audio = {...(state.audio), ...audio}
    },
    setAudioTime(state, payload){
      // console.log("当前点击的歌曲时间currentLength",payload)
			state.audio.currentLength = payload
		},
		setCurrent(state, flag){
			state.audio.currentFlag = flag
		},
    toggleAudioLoadding: (state, flag) => {
			state.audioLoadding = flag
    },
    isPlay(state,payload){
      state.isPlay=payload
    },
    showDetailPlayer(state,payload){
      state.detailPlayerFlag = payload
    },
<<<<<<< HEAD
    showTitle(state,payload){
      state.isTitle=payload
    },
    showBottomBar(state,payload){
      state.bottomBar=payload
    }
=======
    showMenu(state,payload){
      state.menu = payload
    },
    
>>>>>>> 7dd98ad4f10b0d0fc1a7994a24c127972341b2d9
  },
  actions: { 
    setHeadNav:(context,nav)=>{
      context.commit('setHeadNav',nav)
    },
    getSong(context, hash){
			context.commit('toggleAudioLoadding', true)
      axios.get(`/bproxy/yy/index.php?r=play/getdata&hash=${hash}`)
           .then(({data}) => {
            data = data.data
            const songUrl = data.play_url
            const imgUrl = data.img
            const title = data.audio_name
            const songLength = data.timelength / 1000
            const singer = data.author_name
            const currentLength = 0;
            const lrc = data.lyrics
            const song_name = data.song_name
            const	audio = {songUrl, imgUrl, title, singer, songLength, currentLength,lrc,song_name}
            // console.log("这里是data数据:",data,"这里是audio数据:", audio)
            context.commit('setAudio', audio)
            context.commit('setLrc', lrc)
            context.commit('toggleAudioLoadding', false)
          })
    },
          next({dispatch,state}){
            var list=state.listInfo.songList
            // console.log(state.listInfo.songIndex)
            if(state.listInfo.songIndex==list.length-1){
              state.listInfo.songIndex=0
            }else{
              ++state.listInfo.songIndex
            }
            var hash=list[state.listInfo.songIndex].hash
            dispatch('getSong',hash)
          },
          prve({dispatch,state}){
            var list=state.listInfo.songList
            // console.log(state.listInfo.songIndex)
            if(state.listInfo.songIndex==list.length-1){
              state.listInfo.songIndex=0
            }else{
             --state.listInfo.songIndex
            }
            var hash=list[state.listInfo.songIndex].hash
            dispatch('getSong',hash)
          }
  },
  getters:{
    userInfo:state=>
       state.userInfo
    ,
    headNav1:state=>state.headNav1,
    audio: state => state.audio,
		head: state => state.head,
		audioLoadding: state => state.audioLoadding,
		detailPlayerFlag: state => state.detailPlayerFlag,
		showPlayer: state => state.showPlayer,
    isPlay: state => state.isPlay,
    listInfo:state=>state.listInfo,
<<<<<<< HEAD
    isTitle:state=>state.isTitle,
    bottomBar:state=>state.bottomBar
=======
    menu:state=>state.menu,
>>>>>>> 7dd98ad4f10b0d0fc1a7994a24c127972341b2d9
  }
});
